package com.wuyou.sso.common.component;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.wuyou.sso.common.contant.CommonConstants;
import com.wuyou.sso.common.excption.CustomizedAuth2Exception;

import java.io.IOException;

/**
 * @author lishangbu
 * @date 2018/11/16
 * <p>OAuth2 异常格式化
 */
public class CustomizedOAuth2ExceptionSerializer extends StdSerializer<CustomizedAuth2Exception> {

    private static final long serialVersionUID = 1L;

    public CustomizedOAuth2ExceptionSerializer() {
        super(CustomizedAuth2Exception.class);
    }

    @Override
    public void serialize(
            CustomizedAuth2Exception value, JsonGenerator gen, SerializerProvider provider)
            throws IOException {
        gen.writeStartObject();
        gen.writeObjectField("code", CommonConstants.FAIL);
        gen.writeStringField("msg", value.getMessage());
        gen.writeStringField("data", value.getErrorCode());
        gen.writeEndObject();
    }
}
